// 内存对齐机制
// https://www.golangroadmap.com/class/gointerview/8-3.html#%E7%BB%93%E6%9E%84%E4%BD%93%E5%AF%B9%E9%BD%90
package main

import (
	"fmt"
	"runtime"
	"unsafe"
)

type T1 struct {
	i16  int16 // 2 byte
	bool bool  // 1 byte
}

type T2 struct {
	i8  int8  // 1 byte
	i64 int64 // 8 byte
	i32 int32 // 4 byte
}

type T3 struct {
	i8  int8  // 1 byte
	i32 int32 // 4 byte
	i64 int64 // 8 byte
}

func main() {
	var a int16
	fmt.Println(unsafe.Sizeof(a))
	fmt.Printf("bool alignof is %d\n", unsafe.Alignof(bool(true)))
	fmt.Printf("string alignof is %d\n", unsafe.Alignof(string("a")))
	fmt.Printf("int alignof is %d\n", unsafe.Alignof(int(0)))
	fmt.Printf("float alignof is %d\n", unsafe.Alignof(float64(0)))
	fmt.Printf("int32 alignof is %d\n", unsafe.Alignof(int32(0)))
	fmt.Printf("float32 alignof is %d\n", unsafe.Alignof(float32(0)))

	structAlign()
}

func structAlign() {
	fmt.Println(runtime.GOARCH) // amd64

	t1 := T1{}
	fmt.Println(unsafe.Sizeof(t1)) // 4 bytes

	t2 := T2{}
	fmt.Println(unsafe.Sizeof(t2)) // 24 bytes

	t3 := T3{}
	fmt.Println(unsafe.Sizeof(t3)) // 16 bytes
}
